﻿@inherits BaseRelationEditor

@if (Options != null && SelectedOptions != null)
{
    var index = 0;

    <div class="form-control form-control-select-list @(CssHelper.GetValidationClass(State))">
        @foreach (var option in Options)
        {
            index++;

            var check = SelectedOptions.Any(x => x.Id.Equals(option.Id));

            <div class="form-check">
                <div class="row">
                    <div class="col">
                        <label class="form-check-label" for="@group-option@(index)">
                            <input class="form-check-input"
                                   type="checkbox"
                                   value=@option.Id
                                   id="@group-option@(index)"
                                   name=@group
                                   checked=@check
                                   disabled=@IsDisabled
                                   @onchange=@((ChangeEventArgs __e) => OnChangeAsync(__e, option.Id)) />

                            @option.Labels.First()
                        </label>
                    </div>
                    @foreach (var label in option.Labels.Skip(1))
                    {
                        <div class="col">
                            <label class="form-check-label" for="@group-option@(index)">
                                @label
                            </label>
                        </div>
                    }
                </div>
            </div>
        }

    </div>

    @foreach (var message in GetValidationMessages())
    {
        <div class="invalid-feedback">
            @message
        </div>
    }
}
@code {

    private string group = Guid.NewGuid().ToString().Replace("-", "");

    private IEnumerable<IElement>? Options;
    private IEnumerable<IElement>? SelectedOptions;

    protected override async Task OnInitializedAsync()
    {
        if (RelationDataCollection != null)
        {
            RelationDataCollection.OnDataChange += async (s, a) => await UpdateOptionsAsync();

            await RelationDataCollection.SetEntityAsync(EditContext, Parent);
            await UpdateOptionsAsync();

            SelectedOptions = await RelationDataCollection.GetRelatedElementsAsync();
        }
    }

    private async Task UpdateOptionsAsync()
    {
        if (RelationDataCollection == null)
        {
            return;
        }

        await InvokeAsync(async () =>
        {
            Options = await RelationDataCollection.GetAvailableElementsAsync();
            StateHasChanged();
        });
    }

    private async Task OnChangeAsync(ChangeEventArgs e, object value)
    {
        var element = Options.First(x => x.Id.Equals(value));

        if ((bool)e.Value == true)
        {
            await RelationDataCollection!.AddElementAsync(element);
        }
        else
        {
            await RelationDataCollection!.RemoveElementAsync(element);
        }

        EditContext.NotifyPropertyChanged(Property);
    }
}